package com.xbai.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @author xbai
 * @Date 2021/3/17
 */
public class CounterInterceptor implements ProducerInterceptor<String, String> {

    private Integer errorCounter = 0;

    private Integer successCounter = 0;

    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {
        return producerRecord;
    }

    @Override
    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
        if (e == null) {
            successCounter ++;
        } else {
            errorCounter ++;
        }
    }

    @Override
    public void close() {
        System.out.println("success sent : " + successCounter);
        System.out.println("error sent : " + errorCounter);
    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
